#!/bin/sh
#
# @license   http://www.gnu.org/licenses/gpl.html GPL Version 3
# @author    Ian Moore <imooreyahoo@gmail.com>
# @copyright Copyright (c) 2011 Ian Moore
#
# This file is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this file. If not, see <http://www.gnu.org/licenses/>.

set -e

. /etc/default/openmediavault
. /usr/share/openmediavault/scripts/helper-functions


MINIDLNA_CONFIG="/etc/minidlna.conf"

# Enable/disable service
if [ "$(omv_config_get "//services/minidlna/enable")" != "1" ]; then
	update-rc.d minidlna disable
	exit 0
fi

# Create minidlna config file
cat <<EOF > ${MINIDLNA_CONFIG}
# port for HTTP (descriptions, SOAP, media transfer) traffic
port=$(omv_config_get "//services/minidlna/port")

# network interfaces to serve, comma delimited
#network_interface=eth0

# set this if you want to customize the name that shows up on your clients
friendly_name=$(omv_config_get "//services/minidlna/name")

# set this if you would like to specify the directory where you want MiniDLNA to store its database and album art cache
#db_dir=/var/cache/minidlna

# set this if you would like to specify the directory where you want MiniDLNA to store its log file
#log_dir=/var/log

# this should be a list of file names to check for when searching for album art
# note: names should be delimited with a forward slash ("/")
album_art_names=Cover.jpg/cover.jpg/AlbumArtSmall.jpg/albumartsmall.jpg/AlbumArt.jpg/albumart.jpg/Album.jpg/album.jpg/Folder.jpg/folder.jpg/Thumb.jpg/thumb.jpg

# set this to no to disable inotify monitoring to automatically discover new files
# note: the default is yes
inotify=yes

# set this to yes to enable support for streaming .jpg and .mp3 files to a TiVo supporting HMO
enable_tivo=$( if [ `omv_config_get "//services/minidlna/tivo"` = "1" ]; then echo "yes"; else echo "no"; fi)

# set this to strictly adhere to DLNA standards.
# * This will allow server-side downscaling of very large JPEG images,
#   which may hurt JPEG serving performance on (at least) Sony DLNA products.
strict_dlna=$( if [ `omv_config_get "//services/minidlna/strict"` = "1" ]; then echo "yes"; else echo "no"; fi)

# default presentation url is http address on port 80
#presentation_url=http://www.mylan/index.php

# notify interval in seconds. default is 895 seconds.
notify_interval=60

# serial and model number the daemon will report to clients
# in its XML description
serial=31446133
model_number=1

# use different container as root of the tree
# possible values:
#   + "." - use standard container (this is the default)
#   + "B" - "Browse Directory"
#   + "M" - "Music"
#   + "V" - "Video"
#   + "P" - "Pictures"
# if you specify "B" and client device is audio-only then "Music/Folders" will be used as root
#root_container=.

# set this to the directory you want scanned.
# * if have multiple directories, you can have multiple media_dir= lines
# * if you want to restrict a media_dir to a specific content type, you
#   can prepend the type, followed by a comma, to the directory:
#   + "A" for audio  (eg. media_dir=A,/home/jmaggard/Music)
#   + "V" for video  (eg. media_dir=V,/home/jmaggard/Videos)
#   + "P" for images (eg. media_dir=P,/home/jmaggard/Pictures)
EOF

# Process media folders
index=$(omv_config_get_count "//services/minidlna/folders/folder")
while [ ${index} -gt 0 ]
do

	   # Get the shared folder reference and path
    sfref=$(omv_config_get "//services/minidlna/folders/folder[position()=${index}]/sfref")
    mtype=$(omv_config_get "//services/minidlna/folders/folder[position()=${index}]/mtype")
    sfpath=$(omv_get_sharedfolder_path "${sfref}")

	if [ "${mtype}" != "" ]; then
		mtype="${mtype},"
	fi

	if [ "${sfpath}" != "" ]; then
		echo "media_dir=${mtype}${sfpath}" >>${MINIDLNA_CONFIG}
	fi

	index=$(( ${index} - 1 ))

done

update-rc.d minidlna enable
update-rc.d minidlna defaults
